//
//  MYGWhoSeeController.swift
//  MYGrow
//
//  Created by 小火科技 on 2024/3/19.
//

import UIKit

class MYGWhoSeeController: MYGSliderController {
    var selectAction: ((MYGWhoSeeModel) -> Void)?
    private var contents: [MYGWhoSeeModel] = []
    private var selIndex = 0
    // MARK: - life cycle
    override func viewDidLoad() {
        super.viewDidLoad()

        configUI()
        configCell()
    }
    
    private func configCell() {
        contents = []
        let openModel = MYGWhoSeeModel()
        openModel.title = "公开"
        openModel.name = "所有人可见"
        openModel.value = 0
        contents.append(openModel)
        
        let privateModel = MYGWhoSeeModel()
        privateModel.title = "私密"
        privateModel.name = "仅自己可见"
        privateModel.value = 1
        contents.append(privateModel)
        
        let strModel = MYGWhoSeeModel()
        strModel.title = "不给谁看"
        strModel.name = "陌生人"
        strModel.value = 2
        contents.append(strModel)
        
        let fanModel = MYGWhoSeeModel()
        fanModel.name = "粉丝"
        fanModel.value = 3
        contents.append(fanModel)
        
        let careModel = MYGWhoSeeModel()
        careModel.name = "关注"
        careModel.value = 4
        contents.append(careModel)
        
        let hModel = MYGWhoSeeModel()
        hModel.name = "我的患者"
        hModel.value = 5
        contents.append(hModel)
        
        let friendModel = MYGWhoSeeModel()
        friendModel.name = "我的好友"
        friendModel.value = 6
        contents.append(friendModel)
        
        tableView.reloadData()
    }
    
    private func configUI() {
        mainView.addSubview(nameLabel)
        mainView.addSubview(tableView)
        
        nameLabel.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalToSuperview().offset(16)
        }
        
        tableView.snp.makeConstraints { make in
            make.left.right.bottom.equalToSuperview()
            make.width.equalTo(screenWidth)
            make.height.equalTo(331+bottomSafeheight)
            make.top.equalTo(nameLabel.snp.bottom).offset(7)
        }
        
        nameLabel.setContentHuggingPriority(.required, for: .vertical)
        nameLabel.setContentCompressionResistancePriority(.required, for: .vertical)
    }
    
    // MARK: - UI
    private lazy var tableView: UITableView = {
        let tableV = UITableView()
        tableV.separatorStyle = .none
        tableV.delegate = self
        tableV.dataSource = self
        tableV.register(cellWithClass: MYGWhoSeeCell.self)
        return tableV
    }()

    private lazy var nameLabel: UILabel = {
        let label = UILabel()
        label.font = .regular(17)
        label.textColor = .rgba(0, 0, 0, 1)
        label.text = "谁可以看"
        return label
    }()
}

extension MYGWhoSeeController: UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        contents.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withClass: MYGWhoSeeCell.self, for: indexPath)
        let model = contents[indexPath.row]
        if indexPath.row == selIndex {
            model.isSel = true
        } else {
            model.isSel = false
        }
        
        cell.model = model
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        selIndex = indexPath.row
        tableView.reloadData()
        
        selectAction?(contents[selIndex])
        self.dismiss(animated: true)
        
    }
}
